package com.ruoyi.web.controller.mall;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeEntity;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.MallCategory;
import com.ruoyi.mall.service.MallCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/mall/category")
public class MallCategoryController extends BaseController {
    @Autowired
    private MallCategoryService mallCategoryService;

    /**
     * 查询商品分类列表
     */
    @ApiOperation("查询商品分类列表-树形结构")
    @PreAuthorize("@ss.hasPermi('mall:category:list')")
    @PostMapping("/list")
    public AjaxResult list(MallCategory mallCategory) {
        List<MallCategory> list = mallCategoryService.selectMallCategoryList(mallCategory);
        Map<Long, List<MallCategory>> groupMap = list.stream().collect(Collectors.groupingBy(TreeEntity::getParentId));
        list.forEach(entity -> entity.setChildren(groupMap.getOrDefault(entity.getCateId(), new ArrayList<>())));
        return success(list.stream().filter(entity -> entity.getParentId() == null || entity.getParentId().equals(0L)).collect(Collectors.toList()));
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation("获取商品分类详细信息")
    @PreAuthorize("@ss.hasPermi('mall:category:query')")
    @PostMapping(value = "getInfo/{cateId}")
    public AjaxResult getInfo(@PathVariable("cateId") Long cateId) {
        return success(mallCategoryService.selectMallCategoryByCateId(cateId));
    }

    @ApiOperation("下拉树选择框")
    @PostMapping("/treeSelect")
    public List<TreeSelect> treeSelect(MallCategory category) {
        List<MallCategory> categories = mallCategoryService.selectMallCategoryList(category);
        return mallCategoryService.buildTreeSelect(categories);
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("保存商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.SAVE)
    @PostMapping("save")
    public AjaxResult save(@RequestBody MallCategory mallCategory) {
        if (mallCategory.getCateId() == null) {
            return toAjax(mallCategoryService.insertMallCategory(mallCategory));
        } else {
            return toAjax(mallCategoryService.updateMallCategory(mallCategory));
        }
    }

    /**
     * 删除商品分类
     */
    @ApiOperation("删除商品分类")
    @PreAuthorize("@ss.hasPermi('mall:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PostMapping("remove/{cateIds}")
    public AjaxResult remove(@PathVariable Long[] cateIds) {
        return toAjax(mallCategoryService.deleteMallCategoryByCateIds(cateIds));
    }
}
